package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Spot;
import cn.wolfcode.wolf2w.repository.SpotRepository;
import cn.wolfcode.wolf2w.service.ISpotService;
import cn.wolfcode.wolf2w.service.ISpotService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional
public class SpotServiceImpl implements ISpotService {

    //操作MongoDB数据库
    @Autowired
    private SpotRepository spotRepository;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(Spot spot) {
        spotRepository.save(spot);
    }

    @Override
    public void delete(String id) {
        spotRepository.deleteById(id);
    }

    @Override
    public void update(Spot spot) {
        spotRepository.save(spot);
    }


    @Override
    public Spot findByOne(String id) {
        return spotRepository.findById(id).orElse(null);
    }


    /**
     * 高查
     * @param
     * @return
     */
    /*@Override
    public Object query(SpotQuery qo) {
        //创建查询对象
        Query query = new Query();
        //数据总条数
        long count = mongoTemplate.count(query, Spot.class);
        if (count==0) {
            //如果没有数据,返回空Page对象
            return Page.empty();
        }
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        //添加页请求对象
        query.with(pageRequest);
        //查询数据库
        List<Spot> list = mongoTemplate.find(query, Spot.class);
        return new PageImpl<>(list,pageRequest,count);
    }*/

    @Override
    public List<Spot> findAll() {
        return spotRepository.findAll();
    }
}
